package com.wiloon.java8x.io.nio;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.*;

/**
 * Created by roy on 4/21/17;
 */
public class CharSetX {
    public static void main(String[] args) {
        String input = "你123好";
        Charset charset = Charset.forName("ISO-8859-1");
        CharsetEncoder encoder = charset.newEncoder();
        encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
        CharsetDecoder decoder = charset.newDecoder();
        CharBuffer buffer = CharBuffer.allocate(32);
        buffer.put(input);
        buffer.flip();
        try {
            ByteBuffer byteBuffer = encoder.encode(buffer);
            CharBuffer cbuf = decoder.decode(byteBuffer);
            System.out.println(cbuf);  //输出123
        } catch (CharacterCodingException e) {
            e.printStackTrace();
        }

    }
}
